using Cysharp.Threading.Tasks;
using DG.Tweening;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UINetLock : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UINetLock),
            layer = EUILayer.Loading,
            permanent = true,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UINetLock
        {
            #region ObjectBinding Generate 
            public GameObject panel { protected set; get; }
            public UnityEngine.RectTransform image { protected set; get; }
            public IQIGame.Onigao.Framework.ExText text { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("panel", out var __tbv0);
                this.panel = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.RectTransform>("image", out var __tbv1);
                this.image = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("text", out var __tbv2);
                this.text = __tbv2;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        protected UIB_UINetLock ui { get; set; }
        private UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;
        private Transform tweenerTrans => this.ui.image.transform;

        private static System.Func<bool> needRecover = NetLockModule.Instance.IsConnecting;

        private uint _delayShowTimer;
        private uint _outTimeTimer;
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UINetLock();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            RegisterNeedRecoverFunc(needRecover); //用字段避免GC
            this.ui.panel.SetActive(false);
        }

        protected override void OnShow(UIBaseData data)
        {
            StopTimer();
            //并行开启两个Timer，一个延迟显示图案（转菊花），一个检查超时
            DelayShowAnimatedImage().Forget();
            CheckOutTime().Forget();
        }

        private void StopTimer()
        {
            if (_delayShowTimer != 0)
            {
                _delayShowTimer.StopTimer();
                _delayShowTimer = 0;
            }
            if (_outTimeTimer != 0)
            {
                _outTimeTimer.StopTimer();
                _outTimeTimer = 0;
            }
        }

        private async UniTaskVoid DelayShowAnimatedImage()
        {
            _delayShowTimer = ManagerCenter.Timer.StartOneTimer(NetLockModule.NetLockDelayShow, TimerType.Time);
            await _delayShowTimer.AwaitTimer();
            this.ui.panel.SetActive(true);
            Tweener tweener = tweenerTrans.DOLocalRotate(new Vector3(0, 0, 360), 2, RotateMode.FastBeyond360);
            await tweener.SetEase(Ease.Linear).SetLoops(-1).ToUniTask();
        }

        private async UniTaskVoid CheckOutTime()
        {
            _outTimeTimer = ManagerCenter.Timer.StartOneTimer(NetLockModule.NetLockTimeout, TimerType.Time);
            await _outTimeTimer.AwaitTimer();
            Close();
        }

        protected override void OnHide()
        {
            this.ui.panel.SetActive(false);
            tweenerTrans.DOKill();
            StopTimer();
        }

        protected override void OnDispose()
        {

        }
    }
}
